package com.woniuxy.util;

import java.io.IOException;
          import java.util.ArrayList;
          import java.util.List;
        
          import org.apache.http.HttpResponse;
          import org.apache.http.HttpStatus;
          import org.apache.http.NameValuePair;
          import org.apache.http.client.ClientProtocolException;
          import org.apache.http.client.HttpClient;
          import org.apache.http.client.entity.UrlEncodedFormEntity;
          import org.apache.http.client.methods.HttpPost;
          import org.apache.http.impl.client.DefaultHttpClient;
          import org.apache.http.message.BasicNameValuePair;
          import org.apache.http.util.EntityUtils;
        
          public class SendCommonPostMail {

            public static String SendMail(String address) throws  IOException {
              final String url = "https://api.sendcloud.net/apiv2/mail/send";
              final String apiUser = "sc_mcofcp_test_7NQPvG";
              final String apiKey = "08755f5ecc69201222865d274b583965";

              HttpClient httpclient = new DefaultHttpClient();
              HttpPost httPost = new HttpPost(url);

              List params = new ArrayList();
              // 您需要登录SendCloud创建API_USER，使用API_USER和API_KEY才可以进行邮件的发送。
              params.add(new BasicNameValuePair("apiUser", apiUser));
              params.add(new BasicNameValuePair("apiKey", apiKey));
              params.add(new BasicNameValuePair("from", "service@sendcloud.im"));
              params.add(new BasicNameValuePair("fromName", ""));
              params.add(new BasicNameValuePair("to", address));
              params.add(new BasicNameValuePair("subject", "你好啊！"));
                String loginCode = LoginCode.getLoginCode();

                params.add(new BasicNameValuePair("html", "您本次登录的验证码是："+loginCode));

              httPost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
              // 请求
              HttpResponse response = httpclient.execute(httPost);
              // 处理响应
              if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { // 正常返回
                // 读取xml文档
                String result = EntityUtils.toString(response.getEntity());
                System.out.println(result);
                return loginCode;
              } else {
                System.err.println("error");
              }
              httPost.releaseConnection();
              return null;
            }
          }